'use strict';

const Service = require('egg').Service;
const uuid = require('uuid');
class SubscribeService extends Service {
  async addSubscribe (subscribe, transaction) {
    console.log('-----------------addSubscribe----------------------');
    subscribe.id = uuid.v1().replace(new RegExp('-', 'gm'), '');
    subscribe.subscribe_time = new Date();
    subscribe = await this.app.model.Subscribe.create(subscribe, { transaction });
    return subscribe;
  }
  async cancelSub (subscribe) {
    console.log('-----------------cancelSub----------------------');
    const result = await this.app.model.Subscribe.destroy({
      where: {
        user_id: subscribe.user_id,
        sub_id: subscribe.sub_id,
      }
    });
    return result;
  }
  async getSubscribe (user_id, sub_id, transaction) {
    console.log('-----------------getSubscribe----------------------');
    const result = await this.app.model.Subscribe.findOne({
      where: {
        user_id,
        sub_id,
      }, transaction
    });
    return result;
  }
}

module.exports = SubscribeService;
